﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PSSMS.View.AccPay;

namespace PSSMS.View.SysManage
{
    /// <summary>
    /// ClientSM.xaml 的交互逻辑
    /// </summary>
    public partial class ClientSM : UserControl
    {
        BLL.Clientsm.ClientsmClient myClient = new BLL.Clientsm.ClientsmClient();
        //实例化表格：接收筛选数据
        DataTable dtNew = new DataTable();
        public ClientSM()
        {
            InitializeComponent();
        }
      
        /// <summary>
        /// 用户控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Clientsm_Loaded(object sender, RoutedEventArgs e)
        {
            //查询客户信息
            Clientsm();
        }

        private void Clientsm()
        {
            //调用服务查询方法
            dtNew = myClient.CheckClientsm().Tables[0];
            DgrdModel.ItemsSource = dtNew.DefaultView;
        }
        /// <summary>
        /// 选中行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgrdModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //判断是否选中数据
            if (DgrdModel.SelectedItem != null)
            {
                //操作按钮
                btnReset.IsEnabled = true;//启用修改按钮
                btnReset.Opacity = 1;
                btnSave.IsEnabled = true;//启用删除按钮
                btnSave.Opacity = 1;
            }
        }
        /// <summary>
        /// 客户增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClientAdd(object sender, RoutedEventArgs e)
        {
            CleAdd cleA = new CleAdd();
            cleA.Show();
            Clientsm();//刷新数据
        }

        /// <summary>
        /// 打开修改窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取表格选中行
                    DataRowView data = (DataRowView)DgrdModel.SelectedItem;
                    //3、实例化修改窗口创建对象
                    ClientSM_xg padd = new ClientSM_xg(data);
                    padd.ShowDialog();//打开窗口
                    Clientsm();//刷新数据
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Excel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断表格是否有数据
                if (DgrdModel.Items.Count > 0)
                {
                    //调用导出方法
                    Tools.Utils.ExportToExcel.Export(DgrdModel, "供货商信息");
                }
                else
                {
                    MessageBox.Show("表格无数据！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Stop);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取选中行
                    DataRowView rowView = (DataRowView)DgrdModel.SelectedItem;
                    //3、提取单元格获取主键
                    int intCustomerID = Convert.ToInt32(rowView.Row["customerID"]);
                    //4、调用方法执行删除
                    int intCount = myClient.DelectStaff(intCustomerID);
                    if (intCount > 0)
                    {
                        //5、提示
                        MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        //6、刷新
                        Clientsm();//刷新数据
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void print_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断表格是否有数据
                if (DgrdModel.Items.Count > 0)
                {
                    DY_ClientSM_kh Dy = new DY_ClientSM_kh(dtNew);
                    Dy.ShowDialog();//打开窗口
                }
                else
                {
                    MessageBox.Show("表格无数据！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Stop);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            DR_ghsPr mydaoru = new DR_ghsPr();
            mydaoru.ShowDialog();
            //刷新数据
            Clientsm();
        }
    }
}
